﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;

namespace WindowsFormsApplication1
{
    public static class UserLog
    {
        public static string connectionstring;

        public static string[] usermanagerheader = new string[] { "用户号码", "姓名", "性别", "权限", "身份证号码", "出生日期", "联系电话", "电子邮件", "工作地址", "家庭地址", "备注" };
        public static string[] usermanagermodifyheader = new string[] { "用户号码", "姓名", "密码", "性别", "权限", "身份证号码", "出生日期", "联系电话", "电子邮件", "工作地址", "家庭地址", "备注" };
        public static string[] userauthlogheader = new string[] { "数据上报", "指定设备号", "复位设备", "指定IP地址端口号", "智能门锁" };

        public static string[] formcard = new string[] { "序号", "卡号", "状态", "在用"};
        public static string[] phonenum = new string[] { "序号", "手机号" };
        public static string[] carddoor = new[] { "序号", "卡序列号", "门序列号" };
        public static string[] door = new[] { "设备序列号", "监控箱序列号", "设备号", "类型", "开", "在用" };
        public static string[] shake = new[] { "设备序列号", "监控箱序列号", "设备号", "振动值", "1级阀值", "2级阀值", "3级阀值", "状态", "在用" };
        public static string[] temp = new[] { "设备序列号", "监控箱序列号", "设备号", "温度", "1级阀值", "2级阀值", "3级阀值", "状态", "在用" };
        public static string[] humidity = new[] { "设备序列号", "监控箱序列号", "设备号", "湿度", "1级阀值", "2级阀值", "3级阀值", "状态", "在用" };
        public static string[] voltage = new[] { "设备序列号", "监控箱序列号", "设备号", "电压值", "1级阀值", "2级阀值", "3级阀值", "状态", "在用" };

        //添加一条用户登陆日志
        public static void addloginrecord(string id, string name, string role, string oper, string ipaddress)
        {
            if (id == null)
            {
                return;
            }

            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_login item = new tb_login();
            item.Time = DateTime.Now;
            item.ID = id;
            item.Role = role;
            item.Name = name;
            item.Operation = oper;
            item.IPAddress = ipaddress;
            dbcontext.tb_login.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


        public static void addusermanagerecord(string dbuserid, string dbusername, string oper, string userid, string username, string state, string detail)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_usermanage item = new tb_usermanage();
            item.time = DateTime.Now;
            item.dbuserid = dbuserid;
            item.dbusername = dbusername;
            item.oper = oper;
            item.userid = userid;
            item.username = username;
            item.state = state;
            item.detail = detail;
            dbcontext.tb_usermanage.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch
            {
                MessageBox.Show("不能添加用户管理日志记录");
            }
        }


        public static void addrecordfordatagridview(DataTable daorig, DataTable dadest, string[] header, string tablename, string state, int[] checkarray)
        {
        	List<DataRow> rowadd = new List<DataRow> ();
        	List<DataRow> rowdel = new List<DataRow> ();
        	List<DataRow> rowmofdest = new List<DataRow> ();
        	List<DataRow> rowmoforig = new List<DataRow> ();
        	

            //判断 加了新的data然后放进rowadd
            foreach (DataRow dest in dadest.Rows)
            {
                int flag = 0;
                foreach (DataRow orig in daorig.Rows)
                {
                    if (dest[0].ToString() == orig[0].ToString())
                    {
                        flag = 1;
                        for (int i = 1; i < daorig.Columns.Count; i++)
                        {
                            if (dest[i].ToString() != orig[i].ToString())
                            {
                                rowmofdest.Add(dest);
                                rowmoforig.Add(orig);
                                break;
                            }
                        }
                    }
                }
                if (flag == 0)
                {
                    rowadd.Add(dest);
                }
            }

            //判断 加了新的data然后放进rowadd
            foreach (DataRow orig in daorig.Rows)
            {
                int flag = 0;
                foreach (DataRow dest in dadest.Rows)
                {
                    if (dest[0].ToString() == orig[0].ToString())
                    {
                        flag = 1;
                    }
                }
                if (flag == 0)
                {
                    rowdel.Add(orig);

                }
            }


            if (rowadd.Count != 0)
            {
                string[] temp = new string[header.Length];
                for (int i = 0; i < rowadd.Count; i++)
                {
                    for(int j = 0; j < header.Length; j++)
                    {
                        temp[j] = rowadd[i][j].ToString();
                    }
                    string detail = builddetailstringwithcheckbox(header, "添加", temp, null, checkarray);
                    adduserconfigrecord(User.UserID, User.Username, tablename, "添加", state, detail);
                }
            }
            
            if (rowdel.Count != 0)
            {
                string[] temp = new string[header.Length];
                for (int i = 0; i < rowdel.Count; i++)
                {
                    for(int j = 0; j < header.Length; j++)
                    {
                        temp[j] = rowdel[i][j].ToString();
                    }
                    string detail = builddetailstringwithcheckbox(header, "删除", temp, null, checkarray);
                    adduserconfigrecord(User.UserID, User.Username, tablename, "删除", state, detail);
                }
            }
            
            
            if (rowmoforig.Count != 0)
            {
                string[] temporig = new string[header.Length];
                string[] tempdest = new string[header.Length];
                for (int i = 0; i < rowmoforig.Count; i++)
                {
                    for(int j = 0; j < header.Length; j++)
                    {
                        temporig[j] = rowmoforig[i][j].ToString();
                        tempdest[j] = rowmofdest[i][j].ToString();
                    }
                    string detail = builddetailstringwithcheckbox(header, "修改", tempdest, temporig, checkarray);
                    adduserconfigrecord(User.UserID, User.Username, tablename, "修改", state, detail);
                }
            }
         
               


        }

        public static void adduserconfigrecord(string userid, string username, string tablename, string oper, string state, string detail)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_userconfig item = new tb_userconfig();
            item.time = DateTime.Now;
            item.userid = userid;
            item.name = username;
            item.tablename = tablename;
            item.oper = oper;
            item.state = state;
            item.detail = detail;
            dbcontext.tb_userconfig.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch
            {
                MessageBox.Show("不能添加用户管理日志记录");
            }
        }


        public static string builddetailstring(string[] header, string oper, string[] dest, string[] src)
        {
            StringBuilder detail = new StringBuilder();
            int len = header.Length;

            if (oper == "修改")
            {
                for (int i = 0; i < len; i++)
                {          
                    if (src[i] != dest[i])
                    {
                        if (header[i] != "密码")
                        {
                            detail.Append(header[i] + ":" + dest[i] + " (" + src[i] + ")" + "     ");
                        }
                        else
                        {
                            detail.Append(header[i] + ":" + "更改" + "     ");
                        }
                    }
                    else
                    {
                        if (header[i] != "密码")
                        {
                            detail.Append(header[i] + ":" + dest[i] + "     ");
                        }
                        else
                        {
                            detail.Append(header[i] + ":" + "无更改" + "     ");
                        }

                    }

                }

            }
            else
            {
                for (int i = 0; i < len; i++)
                {
                    detail.Append(header[i] + ":" + dest[i] + "     ");
                }
            }

            return detail.ToString();
        }


        public static string builddetailstringwithcheckbox(string[] header, string oper, string[] dest, string[] src, int[] check)
        {
            StringBuilder detail = new StringBuilder();
            int len = header.Length;

            for (int i = 0; i < len; i++)
            {
                if (check[i] == 1)
                {
                    if (src != null)
                    {
                        if (src[i] == "True")
                        {
                            src[i] = "是";
                        }
                        else
                        {
                            src[i] = "否";
                        }
                    }
                    if (dest[i] == "True")
                    {
                        dest[i] = "是";
                    }
                    else
                    {
                        dest[i] = "否";
                    }
                }
                else
                {
                    if(src != null)
                    {
                    if (src[i] == "")
                    {
                        src[i] = "无";
                    }
                        }
                    if (dest[i] == "")
                    {
                        dest[i] = "无";
                    }
                }
            }

                   

            if (oper == "修改")
            {
                for (int i = 0; i < len; i++)
                {
                    if (src[i] != dest[i])
                    {
                        detail.Append(header[i] + ":" + dest[i] + " (" + src[i] + ")" + "     ");
                    }
                    else
                    {
                        detail.Append(header[i] + ":" + dest[i] + "     ");
                    }

                }

            }
            else
            {
                for (int i = 0; i < len; i++)
                {
                    detail.Append(header[i] + ":" + dest[i] + "     ");
                }
            }

            return detail.ToString();
        }


        public static void adduserauthlog(string authid, string authname, string authedid, string authedname,  string state, string detail)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_authlog item = new tb_authlog();

            item.time = DateTime.Now;
            item.authID = authid;
            item.authname = authname;
            item.authedID = authedid;
            item.authedname = authedname;
            item.state = state;
            item.detail = detail;

            dbcontext.tb_authlog.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch(Exception ex)
            {
                MessageBox.Show("不能添加用户授权日志记录");
            }
        }

        public static void adduseroperlog(string userid, string username, string opertype, string state,
                                          string monitorDevId_Name, string deviceId_Name, string note)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_useroper item = new tb_useroper();
            item.time = DateTime.Now;
            item.userID = userid;
            item.username = username;
            item.opertype = opertype;
            item.state = state;
            item.monitorDev_Name = monitorDevId_Name;
            item.device_Name = deviceId_Name;
            item.Note = note;

            dbcontext.tb_useroper.InsertOnSubmit(item);

            try
            {
                dbcontext.SubmitChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show("不能添加用户操作日志记录");
            }
        }


    }
}
